package org.csu.mypetstore.web.servlet;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.Orders;
import org.csu.mypetstore.service.LogService;
import org.csu.mypetstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "ViewOrdersFromServlet", value = "/ViewOrdersFromServlet")
public class ViewOrdersFromServlet extends HttpServlet {
    //
    private static final String VIEW_ORDER_FORM = "/WEB-INF/jsp/order/OrderForm.jsp";
    private static final String SIGNON = "/WEB-INF/jsp/account/SignonForm.jsp";

    private Account account;
    private Cart cart;
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        HttpSession session=request.getSession();
//        OrderService service=new OrderService();
//        List<Orders> ordersList=new ArrayList<Orders>();
//        ordersList=service.getOrderByUsername(((Account)session.getAttribute("accounts")).getUsername() );
//        session.setAttribute("OrderList",ordersList);
//        request.getRequestDispatcher(VIEW_ORDERS).forward(request,response);
        HttpSession session = request.getSession();
        // 获得购物车
        cart = (Cart) session.getAttribute("cart");
        if (cart == null){
            cart = new Cart();
            session.setAttribute("cart",cart);
        }

        // 获得账户
        account = (Account) session.getAttribute("accounts");
        if(account == null){
            // 跳转到登录界面
            request.getRequestDispatcher(SIGNON).forward(request,response);
        }else {
            // 跳转到 订单 页面
            Orders order = new Orders();
            // 生成订单
            order.initOrder(account,cart);

            session.setAttribute("creditCardTypes",order.getCardtype());
            session.setAttribute("order",order);

            Account account = (Account) session.getAttribute("account");
            if(account != null) {
                String strUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                        + request.getContextPath() + request.getServletPath() + "?" + request.getQueryString();
                LogService logService = new LogService();
                String logInfo = logService.logInfo(" ") + strUrl + "  " + "跳转至订单页面";
                logService.insertLogInfo(account.getUsername(),logInfo);
            }

            request.getRequestDispatcher(VIEW_ORDER_FORM).forward(request,response);
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
